<template>
    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" v-model="open" width="680px" append-to-body @close="close">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="主题" prop="topic">
                <el-input v-model="form.topic" placeholder="请输入主题"/>
            </el-form-item>
            <el-form-item label="消息正文" prop="payload">
                <el-input v-model="form.payload" placeholder="请输入消息正文"/>
            </el-form-item>
            <el-form-item label="消息正文使用的编码方式" prop="encoding">
                <el-select v-model="form.encoding" placeholder="请选择消息正文使用的编码方式">
                    <el-option value="plain" label="plain"></el-option>
                    <el-option value="hex" label="hex"></el-option>
                    <el-option value="base64" label="base64"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="QoS 等级" prop="qos">
                <el-select v-model="form.qos" placeholder="请选择QoS 等级">
                    <el-option value="0" label="QoS 0"></el-option>
                    <el-option value="1" label="QoS 1"></el-option>
                    <el-option value="2" label="QoS 2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否为保留消息" prop="retain">
                <el-select v-model="form.retain" placeholder="请选择是否为保留消息">
                    <el-option value="false" label="不是"></el-option>
                    <el-option value="true" label="是"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {sendMqttInstruction} from '/src/api/device/instruction'

    export default {
        name: "sendInstruction",
        emits: ['dialogStatusChanged'],
        props: {
            open: Boolean,
            title: String,
            clientId: String,
        },
        data() {
            return {
                form: {
                    encoding: "plain",
                    qos: "0",
                    retain: "false",
                },
                rules: {
                    topic: [
                        {required: true, message: "主题不能为空", trigger: "blur"}
                    ],
                    payload: [
                        {required: true, message: "消息不能为空", trigger: "blur"}
                    ],
                    encoding: [
                        {required: true, message: "不能为空", trigger: "blur"}
                    ],
                    qos: [
                        {required: true, message: "不能为空", trigger: "blur"}
                    ],
                    retain: [
                        {required: true, message: "不能为空", trigger: "blur"}
                    ],
                }
            }
        },
        methods: {
            //提交
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid && this.clientId) {
                        this.form.clientId = this.clientId
                        sendMqttInstruction(this.form).then(response => {
                            this.msgSuccess("发送成功")
                            this.close()
                        });
                    }
                })
            },
            //取消
            cancel() {
                this.$emit('dialogStatusChanged', false)
            },
            //关闭
            close() {
                this.$emit('dialogStatusChanged', false)
            },
        },
    }
</script>

<style scoped>

</style>
